package ciyuan.Network;

import ciyuan.Player.Skill.SkillManager;
import net.minecraft.network.PacketBuffer;
import net.minecraftforge.fml.network.NetworkEvent;

import java.util.function.Supplier;

public class SkillCastPacket {

    private final String message;

    public SkillCastPacket(PacketBuffer buffer) {
        message = buffer.readUtf();
    }

    public SkillCastPacket(String slotName) {
        this.message = slotName;
    }

    public void toBytes(PacketBuffer buf) {
        buf.writeUtf(message);
    }

    public void handler(Supplier<NetworkEvent.Context> ctx) {
        if (ctx.get().getDirection().getOriginationSide().isClient()) {
            ctx.get().enqueueWork(() -> {
                SkillManager.callSkill(ctx.get().getSender(), message);
            });
        }
        ctx.get().setPacketHandled(true);
    }

}
